var omenubtns = document.querySelectorAll('.controller span');
var oinputs = document.querySelectorAll('.plug-menu input');
var omemorylists = document.getElementsByClassName('memorylist');
var odelAllBtn = document.getElementsByClassName('delAllBtn')[0];
var otips = document.getElementById('tips');

var todayAdd = '';
var memorylist = []; // ？？用途是？
var omemory = {};    // 接收localStorage数据的

function getTime() {
  var timer = new Date();
  var year = timer.getFullYear();
  var month = timer.getMonth() + 1;
  var date = timer.getDate();
  todayAdd = year + "-" + formatDate(month) + "-" + formatDate(date);
}
function formatDate(data) {
  if (data < 10) {
    return "0" + data;
  } else {
    return data;
  }
}
function getInfo() {
  chrome.tabs.getSelected(null, function(tab) {
    oinputs[0].value = tab.title;
    oinputs[1].value = tab.url;
  });
}

omenubtns[0].onclick = getInfo;
omenubtns[1].onclick = function() {
  oinputs[0].value = '';
  oinputs[1].value = '';
}
omenubtns[2].onclick = function() {
  let s1 = oinputs[0].value.trim();
  let s2 = oinputs[1].value.trim();
  var val = '';
  if (s1 && s2) {
    getTime();
    if(/http{1}/.test(s2)){
      val = '<a href="' + s2 + '" target="_blank" title="【' + todayAdd +'】">' + s1+ '</a>';
    }else{
      val = '<span title="【' + todayAdd +'】' + s2 + '">' + s1 + '</span>';
    }
    otips.innerHTML = '(成功加入“' + val + '”)';
    if (omemory[todayAdd]) {
      omemory[todayAdd] = omemory[todayAdd] + "~" + val;
    } else {
      omemory.origin = todayAdd;
      omemory[todayAdd] = val;
      omemorylists[0].innerHTML = '(今日无回顾数据)';
    }
    localStorage.setItem('memoryObj', JSON.stringify(omemory));
    oinputs[0].value = '';
    oinputs[1].value = '';
  }
}
odelAllBtn.onclick = function(){
  if(window.localStorage && localStorage.getItem('memoryObj')){
    omemory = {};
    localStorage.removeItem('memoryObj');
    otips.innerHTML = '(数据已全部清零)';
    omemorylists[0].innerHTML = '(暂无数据)';
  }
}

function loadMemory() {
  getTime();
  let memoryObjStr = window.localStorage?localStorage.getItem('memoryObj'):'';
  let memoryObj = memoryObjStr?JSON.parse(memoryObjStr):null;
  var lastDate = '';
  var expirestr = '';
  var expirelist = [];
  var liStr = '';
  if (memoryObj) {
    omemory = memoryObj;
    // 取完数据了，接下来是按照遗忘记忆表的日期计算着来显示（只管天数）
    // 5分钟 30分钟 12小时  1天   2天    4天    7天   15天
    let [undefined, year, month, date] = /(\d{4})-(\d{2})-(\d{2})/.exec(todayAdd);
    let newDay = '';
    let num = {
      "15": [16, 13, 16, 15, 16, 15, 16, 16, 15, 16, 15, 16],
      "7": [24, 21, 24, 23, 24, 23, 24, 24, 23, 24, 23, 24],
      "4": [27, 24, 27, 26, 27, 26, 27, 27, 26, 27, 26, 27],
      "2": [29, 26, 29, 28, 29, 28, 29, 29, 28, 29, 28, 29],
      "1": [30, 27, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30]
    }
    let daymemorylist = [1, 2, 4, 7, 15];
    let isDel  = false;

    function isWrite(n, index) {
      // 从今天起，算前1,2,4,7,15天的日期，找对应的内容
      let numlist = num[n];
      let tempstr = '';
      let templist = [];
      // debugger;
      if (date > n) {
        let newdate = date - n;
        if (newdate < 10) {
          newdate = "0" + newdate;
        }
        newDay = year + "-" + month + "-" + newdate;
      } else if (month > 1) {
        let newmonth = month - 1;
        let newdate = date * 1 + numlist[newmonth];
        if (newmonth < 10) {
          newmonth = "0" + newmonth;
        }
        newDay = year + "-" + newmonth + "-" + newdate;
      } else { // 当日份不够减，月份是1月的时候，处理年份
        let newyear = year - 1;
        let newdate = date * 1 + numlist[11];
        newDay = newyear + "-" + 12 + "-" + newdate;
      }
      lastDate = newDay;
      if (omemory[newDay]) {
        memorylist.push(newDay + ' |  ' + omemory[newDay]);
        templist = omemory[newDay].split('~');
        for (let i = 0; i < templist.length; i++) {
          tempstr += '<li>' + templist[i] + '</li>'
        }
      }
      liStr += tempstr;
    }

    for (let i = 0; i < 5; i++) {
      isWrite(daymemorylist[i], i);
    }
    if(liStr.trim()){
      omemorylists[0].innerHTML = liStr;
    }else{
      omemorylists[0].innerHTML = '(今日无回顾数据)';
    }
    
    // 逾期处理
    var time = /(\d{4})-(\d{2})-(\d{2})/.exec(lastDate);
    var d = Date.UTC(time[1], time[2], time[3]);
    for (let key in omemory) {
      if (key !== 'origin') {
        let timer = /(\d{4})-(\d{2})-(\d{2})/.exec(key);
        let t = Date.UTC(timer[1], timer[2], timer[3]);
        let expiretemp = [];
        if (t < d && t - d < 1296000000) { // 数据日期最早在15天前，且不超过30天
          expirelist.push(key);
          expiretemp = omemory[key].split('~');
          for (let k = 0; k < expiretemp.length; k++) {
            expirestr += '<li>' + expiretemp[k] + '</li>'
          }
        }else if(t - d >= 1296000000){
          delete omemory[key];
          isDel  = true;
        }
      }
    }
    if(isDel){
      localStorage.setItem('memoryObj', JSON.stringify(omemory));
    }
    if (expirestr) {
      var oexpireContent = document.getElementsByClassName('expire-content')[0];
      oexpireContent.style.display = 'block';
      omemorylists[1].innerHTML = expirestr;
    } 
  }else{
    omemorylists[0].innerHTML = '(暂无数据)';
  }
}

getInfo();
loadMemory();